package com.ten_4_4.hiring.talent.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ten_4_4.hiring.comm.HiringConstants;
import com.ten_4_4.hiring.comm.util.MoneyUtil;
import com.ten_4_4.hiring.comm.util.TalentUtil;
import com.ten_4_4.hiring.talent.po.ResumePo;
import com.ten_4_4.hiring.talent.po.TalentPo;
import com.ten_4_4.hiring.talent.service.ResumeService;

public class ResumeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private ResumeService resumeService = new ResumeService();
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding(HiringConstants.ENCODE_UTF8);
		String act = request.getParameter("act");
		if("modifyResume".equals(act)) {
			modifyResume(request, response);
		} else if("addResume".equals(act)) {
			addResume(request, response);
		} else if ("deleteResume".equals(act)) {
			deleteResume(request, response);
		} else if ("toResumeDetailPage".equals(act)) {
			toResumeDetailPage(request, response);
		} else if ("toAddResumePage".equals(act)) {
			toAddResumePage(request, response);
		} else if ("modifyIntention".equals(act)) {
			modifyIntention(request, response);
		} else if ("modifyWorkExperience".equals(act)) {
			modifyWorkExperience(request, response);
		} else if ("modifyProjectExperience".equals(act)) {
			modifyProjectExperience(request, response);
		} else if ("modifyEducation".equals(act)) {
			modifyEducation(request, response);
		}
	}

	private void modifyEducation(HttpServletRequest request, HttpServletResponse response) throws IOException {
		int education = Integer.parseInt(request.getParameter("education"));
		int talentId = TalentUtil.getTalentPoIdFromSession(request);
		int row = resumeService.modifyEducation(talentId, education);
		if(row == 1) {
			TalentUtil.getTalentPoFromSession(request).setEducation(education);
			response.getWriter().print("success");
		}
	}
	private void modifyWorkExperience(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String workExperience = request.getParameter("workExperience");
		int resumeId = Integer.parseInt(request.getParameter("resumeId"));
		int row = resumeService.modifyWorkExperience(resumeId, workExperience);
		if(row == 1) {
			response.getWriter().print("success");
		}
	}
	private void modifyProjectExperience(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String projectExperience = request.getParameter("projectExperience");
		int resumeId = Integer.parseInt(request.getParameter("resumeId"));
		int row = resumeService.modifyProjectExperience(resumeId, projectExperience);
		if(row == 1) {
			response.getWriter().print("success");
		}
	}
	private void modifyIntention(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String intention = request.getParameter("intention");
		String salaryStr = request.getParameter("salaryStr");
		int resumeId = Integer.parseInt(request.getParameter("resumeId"));
		String[] salaryArr = salaryStr.split("-");
		int expectMinSalary = MoneyUtil.chineseMoneyConvertToRoman(salaryArr[0]);
		int expectMaxSalary = MoneyUtil.chineseMoneyConvertToRoman(salaryArr[1]);
		int row = resumeService.modifyIntention(resumeId, intention, expectMinSalary, expectMaxSalary);
		if(row == 1) {
			response.getWriter().print("success");
		}
		
	}
	private void addResume(HttpServletRequest request, HttpServletResponse response) throws IOException {
		if(TalentUtil.getTalentPoFromSession(request) == null) {
			response.sendRedirect("TalentServlet?act=toLoginPage");
			return;
		}
		String intention = request.getParameter("intention");
		String salaryStr = request.getParameter("salaryStr");
		String[] salaryArr = salaryStr.split("-");
		int expectMinSalary = MoneyUtil.chineseMoneyConvertToRoman(salaryArr[0]);
		int expectMaxSalary = MoneyUtil.chineseMoneyConvertToRoman(salaryArr[1]);
		String workExperience = request.getParameter("work_experience");
		String projectExperience = request.getParameter("project_experience");
		List<Object> list = new ArrayList<>();
		list.add(TalentUtil.getTalentPoIdFromSession(request));
		list.add(intention);
		list.add(workExperience);
		list.add(projectExperience);
		list.add(expectMinSalary);
		list.add(expectMaxSalary);
		int row = resumeService.addResume(list);
		if(row == 1) {
			response.sendRedirect("TalentServlet?act=showAllResumes");
		} else {
			
		}
	}
	private void toAddResumePage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.getRequestDispatcher("WEB-INF/view/talent/talent-resume-add.jsp").forward(request, response);
	}
	private void toResumeDetailPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		if(TalentUtil.getTalentPoFromSession(request) == null) {
			response.sendRedirect("TalentServlet?act=toLoginPage");
			return;
		}
		String resumeId = request.getParameter("resumeId");
		if(resumeId.matches("\\D+")) {
			// FIXME
			System.out.println("非法访问！");
			return;
		}
		ResumePo resumePo = resumeService.getResumePoById(Integer.parseInt(resumeId));
		request.setAttribute("resumePo", resumePo);
		request.getRequestDispatcher("WEB-INF/view/talent/talent-resume-detail.jsp").forward(request, response);
	}
	private void deleteResume(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		TalentPo talentPo = (TalentPo) TalentUtil.getTalentPoFromSession(request);
		if (talentPo == null) {
			response.sendRedirect("TalentServlet?act=toLoginPage");
			return;
		}
		String resumeId = request.getParameter("resumeId");
		if(resumeId.matches("\\D+")) {
			// FIXME
			System.out.println("非法访问！");
			return;
		}
		int rows = resumeService.delete(Integer.parseInt(resumeId));
		if(rows == 1) {
			request.getRequestDispatcher("TalentServlet?act=showAllResumes").forward(request, response);
		} else {
			response.sendError(500);;
		}
	}
	private void modifyResume(HttpServletRequest request, HttpServletResponse response) {
		
	}

}
